var jqTreeTypeOption = function(){
	this.callType = new Array("D", "P");
	this.returnJson = new Array("jsonFile","jsonManual");
	this.returnBack = new Array("htmlFile","jsonFile","htmlManual","jsonManual");
};

var jqTreeview = function(options){
	this.xWindow = new Browser();
	this.jqTreeOpts = options;
};

jqTreeview.prototype.listInit = function(obj){
	return this.listSetAtBrowser(obj);
};

jqTreeview.prototype.listSetAtBrowser = function(obj){
	var opts = this.jqTreeOpts;
	var collapsed = opts.collapsed;
	var rootKey = opts.jsonRootKey;
	var rootTreeId = opts.rootTreeId;
	var treeColourClass = opts.treeColourClass;
	var canvasId = opts.canvasId;
	var rootUlTag = null;
	
	if(this.xWindow.isIE && this.xWindow.version < 9.0){
		//Browser compatibility: All browser.
		for(key in obj) {
			if(key == rootKey){
				rootUlTag = $("<ul id='"+rootTreeId+"' class='"+treeColourClass+"' />");
				if(obj.hasOwnProperty(key)) {
					for(var o in obj[key]){					
						var item = obj[key][o];						
						var urlTag = (item.url != "undefined" && item.url != null && item.url.replace(/ /g, "")) ? 
								"<span class='noneSpan'>"+item.url+"</span>" : "";
						var classTag = (collapsed)? " class='closed'" : (item.collapse) ? " class='closed'" : "";
						var liTag = "<li "+classTag+" id='"+item.index+"'><span>"+item.name+"</span>"+urlTag+"</li>";
						
						if(parseInt(item.belong) == 0){ $(rootUlTag).append($(liTag));							
						}else{
							var target = $(rootUlTag).find("#"+item.belong);
							$(target).not(":has(> ul)").append("<ul />");
							$(target).find("> ul").append($(liTag));
						}
					}
				}
			}else{ if(key == "error"){ $("#"+canvasId).html(obj[key]); } }
		}
		
	}else{
		//Browser compatibility: Firefox(Gecko)4 | Chrome()2.0) | Internet Explorer9 | Opera12 |  Safari5 
		//Specifications: ECMAScript Language Specification 5.1th Edition (ECMA-262) - Implemented in JavaScript 1.8.5
		Object.keys(obj).forEach(function(key){
			if(key == rootKey){			
				rootUlTag = $("<ul id='"+rootTreeId+"' class='"+treeColourClass+"' />");
				if(obj[key].length){ 
					obj[key].forEach(function(item){
						var urlTag = (item.url != "undefined" && item.url != null && item.url.replace(/ /g, "")) ? 
								"<span class='noneSpan'>"+item.url+"</span>" : "";
						var classTag = (collapsed)? " class='closed'" : (item.collapse) ? " class='closed'" : "";
						var liTag = "<li "+classTag+" id='"+item.index+"'><span>"+item.name+"</span>"+urlTag+"</li>";
						
						if(parseInt(item.belong) == 0){ $(rootUlTag).append($(liTag));							
						}else{
							var target = $(rootUlTag).find("#"+item.belong);
							$(target).not(":has(> ul)").append("<ul />");
							$(target).find("> ul").append($(liTag));
						}
					});
				}
			}else{ if(key == "error"){ $("#"+canvasId).html(obj[key]); } }
		});
	}
	
	return $(rootUlTag);
};

jqTreeview.prototype.treeMenuSet = function(){
	var opts = this.jqTreeOpts;
	var tree = $("#"+opts.rootTreeId).treeview({
		animated: (opts.animated == "undefined" || !opts.animated)?"":opts.animated
		, collapsed: opts.collapsed
		, control: opts.controller
		, persist: (opts.persist == "undefined" || !opts.persist)?"":opts.persist
		, cookieId: (opts.cookieId == "undefined" || !opts.cookieId)?"":opts.cookieId
	});

	var prevObj = null;
	$(tree).find("li").not(":has(> ul)").each(function(){
		var obj = $(this).find("> span");
		if($(obj).length > 1){
			var li = $(this);
			var titleSpan = $(obj).eq(0);
			var urlSpan = $(obj).eq(1);
			var url = $(urlSpan).text().replace(/ /g, "");

			$(titleSpan).hoverClass();
			$(titleSpan).click(function(){
				if(url == "") {
					alert("Not ready yet!"); 
					return;
				}
				if($(prevObj).is($(li))) return;

				if(opts.navi){
					$(prevObj).parents("li").each(function(){
						$(this).find("> span").eq(0).removeClass("menuClickedPerent").removeClass("menuClickedHover");
					});
					$(li).parents("li").each(function(){
						$(this).find("> span").eq(0).addClass("menuClickedPerent").hoverClass("menuClickedHover");
					});
				}

				if($(prevObj).length > 0){ 
					$(prevObj).find("> span").eq(0).removeClass("menuClicked").hoverClass(); 
				}
				$(titleSpan).removeClass().addClass("menuClicked");
				
				loadTreeviewCanvas(url,'POST','html',opts.canvas,'');
				prevObj = $(li);
			});
		}
	});
};

jqTreeview.prototype.compareArray = function(src, tar){
	var rtnValue = false;

	if(this.xWindow.isIE && this.xWindow.version < 9.0){
		for(var i in src){
			if(src[i].indexOf(tar) != -1){
				rtnValue = true;
				break;
			}
		}
	}else{
		if(src.indexOf(tar) != -1){
			rtnValue = true;
		}
	}

	return rtnValue;
};

var loadTreeviewCanvas = function (path, mType, dtype, canvas, fNm) {
	var ajax = new jqMyAjax();
	var rtnBoard = canvas;
	var formNm = (fNm == "undefined")?"":fNm;
	var data = (mType=="POST" && String(formNm).length > 0)?$("#"+fNm).serialize():"";
	var option = new Array();
	option["type"] = mType;
	option["url"] = path;
	option["dataType"] = dtype;
	option["data"] = data;
	option["success"] = function(result){
		$(rtnBoard).html(result);
	};
	option["error"] = function(xhr, status, thrown){
		var extraMsg = "";
		var sessionOutRedirect = true;
		ajax.errorCalback(xhr, extraMsg, sessionOutRedirect);
		
		var style = "style='font-weight:bold; font-size:14pt;'";
		$(rtnBoard).html("<font "+style+">HTTP STATUS "+xhr.status + "  " + status.toUpperCase()+"</font>");
	};
	
	ajax.callAjax(option);
};